package com.xiyue.server.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sport.common.web.controller.BaseController;
import com.sport.common.web.model.PageParams;
import com.sport.common.web.model.Result;
import com.sport.model.dto.Option;
import com.sport.model.entity.FoodType;
import com.xiyue.server.annotation.RestControllerMapping;
import com.xiyue.server.service.FoodTypeService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;

import java.util.List;
import java.util.Map;

/**
 * 食物分类;(food_type)表控制层
 * @author: xiyue
 * @date : 2025-8-14
 */
@Tag(name = "食物分类")
@RestControllerMapping("/foodType")
@ApiSupport(author = "xiyue:chencankun.dev@foxmail.com", order = 9)
@SaCheckRole(value = {"admin","FOOD_ADMIN"},mode =  SaMode.OR)
public class FoodTypeController extends BaseController {

    @Resource
    private FoodTypeService foodTypeService;
    
    @PostMapping("page")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "分页查询")
    public Result<Page<FoodType>> pageQuery(@RequestBody PageParams<FoodType> pageParams) {
        // 1.查询参数
        FoodType params = pageParams.getParams();
        LambdaQueryWrapper<FoodType> lqw = new LambdaQueryWrapper<>();
        // 2.查询条件
        lqw.eq(params.getTitle() != null, FoodType::getTitle, params.getTitle());
        // 3.分页查询
        return Result.success(foodTypeService.page(pageParams.getPage(), lqw));
    }
    
    @PostMapping("save")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "新增数据",
            description = "需要角色 admin or FOOD_ADMIN<br>需要权限 food:type:save")
    @SaCheckPermission(value = {"food:type:save"})
    public Result<Boolean> save(@RequestBody FoodType foodType) {
        return toResult(foodTypeService.saveOrUpdate(foodType));
    }
    
    @GetMapping("/delete/{id}")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "通过主键删除数据",
            description = "需要角色 admin or FOOD_ADMIN<br>需要权限 food:type:delete",
            parameters = {@Parameter(name = "id", description = "主键",
                    required = true, in = ParameterIn.PATH)
    })
    @SaCheckPermission(value = {"food:type:delete"})
    public Result<Boolean> deleteById(@PathVariable String id){

        return toResult(foodTypeService.removeById(id));
    }

    @GetMapping("options")
    @Operation(summary = "查看食品分类列表")
    public  Result<List<Option>> options(){
        QueryWrapper<FoodType> lqw = new QueryWrapper<>();
        lqw.select("title as label","id as value");
        List<Map<String, Object>> maps = foodTypeService.listMaps(lqw);
        List<Option> options = JSONUtil.parseArray(maps).toList(Option.class);
        return Result.success(options);
    }
}